package com.angel.web.health;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.angel.health.BuzCity;
import com.angel.health.BuzMedical;
import com.angel.health.BuzSubjects;
import com.angel.provider.health.BuzMedicalProvider;
import com.angel.provider.health.BuzSubjectsProvider;


/**
 * 体检项目
 * 
 * @author hanlinxiu
 *
 */

@Controller
@RequestMapping("/subjects")
@SessionAttributes("user")
public class BuzSubjectsController {

	@Autowired
	private BuzSubjectsProvider provider;
	
	@Autowired
	private BuzMedicalProvider buzMedicalProvider;
	
	/**
	 * 新增页面
	 * 
	 * @param id
	 * @param from
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toAddSubjects")
	public String toAddItem(Integer id, String from, ModelMap modelMap){
		
		BuzSubjects buzSubjects = null;
		
		if(id != null){
			buzSubjects = provider.findById(id);
		}
		
		//获取所有体检库
		List<Map<String,Object>> medicals = buzMedicalProvider.findAll(new BuzMedical());
		
		modelMap.put("from", from);
		modelMap.put("data", buzSubjects);
		modelMap.put("medicals", medicals);
		
		return "institutions/organization-addSubjects";
	}
	
	/**
	 * 新增，更新体检项目
	 * 
	 * @param buzInstitutions
	 * @return  
	 */
	@RequestMapping("/addSubjects")
	@ResponseBody
	public Map<String, Object> addSubjects(@ModelAttribute("user") String user, @ModelAttribute("buzSubjects")BuzSubjects buzSubjects){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		
		if(buzSubjects.getName()==null || buzSubjects.getName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "体检项目名称不能为空!");
			return map;
		}
			
		buzSubjects.setEnable(1);
		
		if(buzSubjects.getId() == null){
			buzSubjects.setCreateBy(user);
			provider.insert(buzSubjects);
		}else{
			buzSubjects.setUpdateBy(user);
			provider.updateById(buzSubjects);
		}
			
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	@RequestMapping("/getAllSubject")
	@ResponseBody
	public Map<String, Object> getAllSubject(Integer medicalId){
		
		Map<String, Object> map = new HashMap<>();
		
		BuzSubjects buzSubjects = new BuzSubjects();
		buzSubjects.setEnable(1);
		
		List<Map<String,Object>> list = null;
		
		BuzMedical buzMedical = new BuzMedical();
		buzMedical.setId(medicalId);
		if(medicalId != null && medicalId > 0){
			buzSubjects.setMedical(buzMedical);
			list = provider.findAll(buzSubjects);
		}
		
		map.put("list", list);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	@RequestMapping("/deleteSubject")
	@ResponseBody
	public Map<String, Object> deleteSubject(BuzSubjects buzSubjects){
		Map<String, Object> map = new HashMap<>();
		
		provider.delById(buzSubjects);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}

	
}
